Conditional Statements
1. Comparison Operators
Comparison operators are used to compare values. They evaluate to True
or False
depending on the values you compare.
We can't use a single =
sign for comparison because it is reserved for assignment. The comparison operators are:
==
Equal to!=
Not equal to<
Less than>
Greater than<=
Less than or equal to>=
Greater than or equal to
x, y = 3, 5
print(x == y) # Output: False
2. Conditional Statements
1. if
if condition:
# code to execute if condition is True
2. if
- else
if condition:
# code if condition is True
else:
# code if condition is False
3. if
- elif
- else
if condition1:
# code if condition1 is True
elif condition2:
# code if condition2 is True
else:
# code if all conditions are False
age = 25
has_id = True
if age >= 21 and has_id:
print("You can enter the bar and drink.")
elif age >= 18 and has_id:
print("You can enter the bar but can't drink.")
else:
print("You can't enter the bar.")
4. Ternary Operator
The ternary operator in Python is a concise way to write a simple if-else statement in a single line. It's also sometimes called a conditional expression.
result = value_if_true if condition else value_if_false
# Ternary operator example
status = "adult" if age >= 18 else "minor"
print(f"You are a {status}.")
2. if
statement scope
In Python, unlike functions, if
statements do not create a new scope. This means that variables defined inside an if
statement are accessible outside of the if
statement.
if True:
message = "Hello"
print(message) # This will print "Hello"
They can also update variables that were defined outside of the if
statement. Here's an example:
balance = -100
if balance < 0:
balance = 0
print(balance) # This will print 0
Within functions, if
statements have the same scope as the function. This means that variables defined inside an if
statement are accessible within that function, but not outside of it.
def is_balance_low(balance: int):
if balance <= 100:
message = "Warning: Low balance."
print(message)
is_balance_low(50) # This will print "Warning: Low balance."
print(message) # This will cause an error
3. Logical Condition
We can use the or
, and
and not
operators to evaluate expressions into True
or False
. We can use these expressions to execute conditional code blocks.
balance = 500
if balance > 0 and balance < 1000:
print("Balance is between 0 and 1000.")
4. Truthy and Falsy
A value is considered truthy if it evaluates to True
in a boolean context. A value is considered falsy if it evaluates to False
in a boolean context. The condition in an if
statement is considered a boolean context.
A value is falsy if it is:
False
(boolean)None
(NoneType)0
(integer)0.0
(float)""
(empty string)[]
(empty list)- Most other empty collections (e.g. empty tuple, empty set, empty dictionary)
A value is truthy if it is:
True
(boolean)- All integers other than
0
- All floats other than
0.0
- All strings other than
""
- All collections with at least one element
It's possible to use non-boolean values to execute conditional statements.
msg = ""
if msg:
print("Message is not empty.") # This will not be printed
msg = "Hello, World!"
if msg:
print("Message is not empty.") # This will be printed
The following two if
statements are equivalent:
x = 10
if x:
print("x is not zero")
if x != 0:
print("x is not zero")